import CryptoJS from "crypto-js"
export const aseKey = 'dalianligong';
export const zmAesIV= `JL$<&*1d~ld?:#1p`;
export const zmAesKey="{g;,9~88'/sw`SR5";
export const loginIv = "{g;,9~l3'/sw`995";
export const loginKey = "JL$<&*l9~57?:#1p";

// AES 解密(新)
export function aesDecrypt (word,aesKey = zmAesKey,aesIV = zmAesIV) {
  var _key = CryptoJS.enc.Utf8.parse(aesKey),
    _iv = CryptoJS.enc.Utf8.parse(aesIV);
  var decrypted = CryptoJS.AES.decrypt(word, _key, {
    iv: _iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return decrypted.toString(CryptoJS.enc.Utf8);
}
// AES 加密(新)
export function aesMinEncrypt (word,aesKey = zmAesKey,aesIV = zmAesIV) {
  var _word = CryptoJS.enc.Utf8.parse(word),
    _key = CryptoJS.enc.Utf8.parse(aesKey),
    _iv = CryptoJS.enc.Utf8.parse(aesIV);
  var encrypted = CryptoJS.AES.encrypt(_word, _key, {
    iv: _iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.toString();
}

// 加密(旧)
export const encryptByDES = (message, key = aseKey) => {
  const keyHex = CryptoJS.enc.Utf8.parse(key)
  const encrypted = CryptoJS.DES.encrypt(message, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return encrypted.toString()
}
// 解密(旧)
export const decryptByDES = (ciphertext, key = aseKey) => {
  const keyHex = CryptoJS.enc.Utf8.parse(key)
  const decrypted = CryptoJS.DES.decrypt({
    ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
  }, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return decrypted.toString(CryptoJS.enc.Utf8)
}

export function getUrlParam(name) {
  const after = window.location.href
  if (after.indexOf('?') === -1) return null; //如果url中没有传参直接返回空
  // return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [',', ''])[1].replace(/\+/g, '%20')) || ''
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  let r = window.location.search.substr(1).match(reg)
    || window.location.hash.split("?")[1].match(reg)
  if (r != null) {
    return decodeURIComponent(r[2])
  } else {
    return null
  }
}
/**
 * @see 判断当前浏览器环境
 */
 export function Device(key) {
  const { userAgent: UA } = navigator;
  const UA_L = UA.toLowerCase();
  const judgeDevice = {
      trident: UA.includes("Trident"), //IE内核
      presto: UA.includes("Presto"), //opera内核
      iPad: UA.includes("iPad"), //是否iPad
      iPhone: UA.includes("iPhone"), //是否为iPhone或者QQHD浏览器
      webKit: UA.includes("AppleWebKit"), //苹果、谷歌内核
      webApp: UA.indexOf("Safari") === -1, //是否web应用程序，没有头部与底部
      mobile: !!UA.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
      ios: !!UA.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
      android: UA.includes("Android") || UA.includes("Linux"), //android终端或uc浏览器
      gecko: UA.includes("Gecko") && UA.indexOf("KHTML") === -1, //火狐内核
      wechat: UA_L.toLowerCase().match(/MicroMessenger/i) == "micromessenger", // 微信
      wxwork: UA_L.match(/wxwork/i) == "wxwork" // 是否是企业微信
  };
  return judgeDevice[key];
}
export function getSearchString(key) {
  var str = window.location.search;
  str = str.substring(1, str.length); // 获取URL中?之后的字符（去掉第一位的问号）
  // 以&分隔字符串，获得类似name=xiaoli这样的元素数组
  var arr = str.split("&");
  var obj = {};

  // 将每一个数组元素以=分隔并赋给obj对象
  for (var i = 0; i < arr.length; i++) {
      var tmp_arr = arr[i].split("=");
      obj[decodeURIComponent(tmp_arr[0])] = decodeURIComponent(tmp_arr[1]);
  }
  return obj[key];
}

export default {
  install(Vue){
    Vue.prototype.$utils = {
      Device,
      getSearchString,
      getUrlParam,
      encryptByDES,
      decryptByDES,
      aesDecrypt,
      aesMinEncrypt,
      loginIv,
      loginKey,
    }
  }
}